const ipc=window.require('electron').ipcRenderer
var settings = {
  // filePath:  window.globalArgs['home'] +  '/' + 'settings.json',
  // filePath: window.globalArgs['home']  +  '/' + 'settings.json',
  list: {},
  onChangeCallbacks: [],
  save: function(cb) {
    // fs.writeFile(settings.filePath, JSON.stringify(settings.list), function (e) {
    //   if (cb) {
    //     cb()
    //   }
    // })
    // if (process.type === 'renderer') {
    //   ipc.send('receiveSettingsData', settings.list)
    // } else if (process.type === 'browser' && mainWindow) {
    //   mainWindow.webContents.send('receiveSettingsData', settings.list)
    // }
  },
  runChangeCallacks() {
    settings.onChangeCallbacks.forEach(function(listener) {
      if (listener.key) {
        listener.cb(settings.list[listener.key]);
      } else {
        listener.cb();
      }
    });
  },
  get: function(key) {
    return settings.list[key];
  },
  listen: function(key, cb) {
    if (key && cb) {
      cb(settings.get(key));
      settings.onChangeCallbacks.push({ key, cb });
    } else if (key) {
      // global listener
      console.log('push global listener');
      settings.onChangeCallbacks.push({ cb: key });
    }
  },
  set: function(key, value, cb) {
    ipc.send('setSetting', { key: key, value: value });
    settings.list[key] = value;
    // settings.save(cb)
    settings.runChangeCallacks();
  },
  // initialize: function() {
  //   console.log('settings initialize');
  //   // var fileData;
  //   // try {
  //   //   fileData = fs.readFileSync(settings.filePath, 'utf-8')
  //   // } catch (e) {
  //   //   console.warn(e)
  //   // }
  //   // if (fileData) {
  //   //   settings.list = JSON.parse(fileData)
  //   //   console.log(settings.list);
  //   // }
  //   // else{
  //   //   console.log("fileData=null");
  //   // }
  //   settings.list = ipc.sendSync('getSettingsData');

  //   ipc.on('receiveSettingsData', function(e, data) {
  //     settings.list = data;
  //     settings.runChangeCallacks();
  //   });
  // },
};
// console.log("settings==========================")
// console.log(settings)
// settings.initialize()
// export default settings
export default settings;
